package com.iceGame;

import java.util.List;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.tiled.TileAtlas;
import com.badlogic.gdx.graphics.g2d.tiled.TileMapRenderer;
import com.badlogic.gdx.graphics.g2d.tiled.TiledLoader;
import com.badlogic.gdx.graphics.g2d.tiled.TiledMap;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;
import com.sun.opengl.util.TileRenderer;



public class WorldRenderer {
	static final float FRUSTUM_WIDTH = 32;
	static final float FRUSTUM_HEIGHT = 20;
	private static final float HEIGHT_PIXELS_PER_METER = 32;
	private static final float WIDTH_PIXELS_PER_METER = 32;
	
	PhysicsWorld world;
	OrthographicCamera cam;
	SpriteBatch batch;
	TileMapRenderer tileMapRenderer;
	TileAtlas tileAtlas;
	TiledMap tileMap;
	Vector3 tmp = new Vector3(); //don't know why it here for yet
	//test
	TiledMapHelper tiledMapHelper ;

	public WorldRenderer (PhysicsWorld world) {
		this.world = world;
		create();

		
	}

	public void create(){
		
		this.batch = new SpriteBatch();
		this.cam = new OrthographicCamera(FRUSTUM_WIDTH , FRUSTUM_HEIGHT );		
		this.cam.position.set(FRUSTUM_WIDTH / 2, FRUSTUM_HEIGHT / 2, 0);
		/*final String path = "data/world/level1/";
		final String mapname = "level1";
		

		FileHandle mapHandle = Gdx.files.internal(path + mapname + ".tmx");
		FileHandle base2 = Gdx.files.internal("data/world/level1/");
		tileMap = TiledLoader.createMap(mapHandle);
		tileAtlas = new TileAtlas(tileMap, base2);
		
		int blockWidth = 32;
		int blockHeight = 20;
		tileMapRenderer = new TileMapRenderer(tileMap, tileAtlas, blockWidth, blockHeight);
		Gdx.gl.glClearColor(0, 0.5f, 0.95f, 1);
		*/
		
		//liron's change
		tiledMapHelper = new TiledMapHelper(cam);
		tiledMapHelper.setCommonPackFile("data/world/level1");
		tiledMapHelper.setBaseDir("data");

		tiledMapHelper.loadMap(world.currentLevel.mapFile);
		tiledMapHelper.loadCollisions(world.currentLevel.collisionFile, this.world.world,
				HEIGHT_PIXELS_PER_METER,WIDTH_PIXELS_PER_METER);
	}
	
	private void renderTileMap(){
		tiledMapHelper.render();
		
		/*liron's change
		tileMapRenderer.getProjectionMatrix().set(cam.c);
		tmp.set(0, 0, 0);
		cam.unproject(tmp);
		tileMapRenderer.render(tmp.x, tmp.y, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());//, layersList);
		*/
	}
	public void render(){
		Gdx.gl.glClearColor(0, 0.5f, 0.95f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		updateCameraPosition();
		cam.update();
		batch.setProjectionMatrix(cam.combined);
		renderBackground();
		renderTileMap();
		

		batch.begin();	
		renderDestination();
		if (Settings.signsEnabled)
			renderSigns();
		renderIceCube();
		renderCollectibleCubes();
		renderFireballs();
		renderFire();
		renderPlatforms();
		batch.end();
		
		
	}

	private void renderPlatforms() {
		for (MovingPlatform m : world.currentLevel.movingPlatforms){
			batch.draw(Assets.platform,m.body.getPosition().x - 0.5f* m.width , m.body.getPosition().y - 0.5f * m.height, m.width, m.height);
		}
		
	}

	private void renderSigns() {
		List<Sign> signs = world.currentLevel.signs;
		TextureRegion texture;
		for (int i = 0; i < signs.size();i++){
			Sign sign =  signs.get(i);
			switch (sign.typeSign){//1-left 2-right 3-leftUp 4-leftDown 5-rightUp 6-rightDown 7- danger
				case 1:
					texture= Assets.leftSign;
					break;
				case 2:
					texture= Assets.rightSign;
					break;
				case 3:
					texture= Assets.upLeftSign;
					break;
				case 4:
					texture= Assets.downLeftSign;
					break;
				case 5:
					texture= Assets.upRightSign;
					break;
				case 6:
					texture= Assets.downRightSign;
					break;
				case 7:
					texture= Assets.dangerSign;
					break;
				default:
					texture=null;
					break;
			}

            batch.draw(texture,sign.position.x - sign.bounds.width , sign.position.y - sign.bounds.height, sign.bounds.width, sign.bounds.height);
		}
		
	}

	private void renderFireballs(){
		for (Fireball fball : world.currentLevel.fireballs){
			TextureRegion tRegion;
			if (fball.state == Fireball.EXPLODING){
				tRegion = Assets.fireballExplosion.getKeyFrame(fball.stateTime, false);
			}
			else{
				tRegion = Assets.fireballAnimation.getKeyFrame(fball.stateTime, true);
				
			}
			batch.draw(tRegion,fball.body.getPosition().x - 0.5f* fball.width , fball.body.getPosition().y - 0.5f * fball.height, fball.width, fball.height);

		}
	}

	private void renderCollectibleCubes() {
		List<CollectableGameObject> cubes = world.currentLevel.collectables;
		for (int i = 0; i < cubes.size();i++){
			CollectableIce iceCube = (CollectableIce) cubes.get(i); //TODO fix casting
            batch.draw(Assets.CollectableIceCube,iceCube.position.x - 0.5f* iceCube.bounds.width , iceCube.position.y - 0.5f * iceCube.bounds.height, iceCube.bounds.width, iceCube.bounds.height);

		}
	}
	

	private void renderDestination() {
		DestinationObject iglu = world.currentLevel.destination;
		batch.draw(Assets.iglu,iglu.bounds.x,iglu.bounds.y,iglu.bounds.width,iglu.bounds.height);
		
	}

	private void updateCameraPosition() {
		this.cam.position.x = world.currentLevel.iceCube.body.getPosition().x;
		this.cam.position.y = world.currentLevel.iceCube.body.getPosition().y;
		if (cam.position.x < FRUSTUM_WIDTH / 2)
			cam.position.x = FRUSTUM_WIDTH /2;
		if (cam.position.x > world.currentLevel.levelWidth - (FRUSTUM_WIDTH / 2))
			cam.position.x = world.currentLevel.levelWidth - (FRUSTUM_WIDTH / 2);
		if (cam.position.y < FRUSTUM_HEIGHT / 2)
			cam.position.y = FRUSTUM_HEIGHT /2;
		if (cam.position.y > world.currentLevel.levelHeight - (FRUSTUM_HEIGHT / 2))
			cam.position.y = world.currentLevel.levelHeight - (FRUSTUM_HEIGHT / 2);
		tiledMapHelper.updateCamera(this.cam.position);
		
	}

	private void renderBackground() {
		batch.disableBlending();
		batch.begin();	
		batch.draw(Assets.backgroundRegion, cam.position.x - FRUSTUM_WIDTH / 2, cam.position.y - FRUSTUM_HEIGHT / 2, FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
		batch.end();
		batch.enableBlending();
	}
/*
	private void renderPlatform() {
		BasicPlatform platform = world.platform;
		BasicPlatform plat2 = world.plat2;
		batch.draw(Assets.platform, platform.body.getPosition().x - 2.5f, platform.body.getPosition().y - 0.5f , 5, 1);
		batch.draw(Assets.platform, plat2.body.getPosition().x - 3.5f, plat2.body.getPosition().y - 0.5f , 7, 1);
		
	}
*/
	private void renderIceCube() {
		IceCube cube = world.currentLevel.iceCube;
		batch.draw(Assets.iceCube, cube.body.getPosition().x  - 0.5f * cube.bounds.width, cube.body.getPosition().y -0.5f * cube.bounds.height,
				0.5f * cube.bounds.width,0.5f * cube.bounds.height ,
				cube.bounds.width,cube.bounds.height,1, 1,MathUtils.radiansToDegrees * cube.body.getAngle());	
	}
	

	private void renderFire(){
		for (int i = 0;i < world.currentLevel.fires.size();i++){
			Fire f = world.currentLevel.fires.get(i);
			TextureRegion tRegion = Assets.fireAnimation.getKeyFrame(f.stateTime, true);
			batch.draw(tRegion,f.position.x - 0.5f* f.bounds.width , f.position.y - 0.5f * f.bounds.height, f.bounds.width, f.bounds.height);
		}
		
		
	}
}


